package com.qi.spzx.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qi.spzx.model.dto.h5.ProductSkuDto;
import com.qi.spzx.model.entity.product.Product;
import com.qi.spzx.model.entity.product.ProductDetails;
import com.qi.spzx.model.entity.product.ProductSku;
import com.qi.spzx.model.vo.h5.ProductItemVo;
import com.qi.spzx.product.mapper.ProductDetailsMapper;
import com.qi.spzx.product.mapper.ProductMapper;
import com.qi.spzx.product.mapper.ProductSkuMapper;
import com.qi.spzx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2024/2/25 17:12
 * @Version 1.0.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public List<ProductSku> findProductSkuBySale() {
        List<ProductSku> productSkuBySale = productSkuMapper.findProductSkuBySale();
        return productSkuBySale;
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page, limit);
        List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    @Override
    public ProductItemVo item(Long skuId) {
        //创建一个返回类
        ProductItemVo result = new ProductItemVo();
        //首先根据skuid查询出基本的信息
        ProductSku productSku = productSkuMapper.selectBySkuId(skuId);
        if (ObjectUtil.isNotEmpty(productSku)) {
            result.setProductSku(productSku);
        }
        //将商品信息进行处理
        Long productId = productSku.getProductId();
        Product product = productMapper.selectById(productId);
        if (ObjectUtil.isNotEmpty(product)) {
            result.setProduct(product);
            //设置商品轮播图列表
            String[] split = product.getSliderUrls().split(",");
            result.setSliderUrlList(Arrays.asList(split));
            //设置商品规格信息
            String specValue = product.getSpecValue();
            result.setSpecValueList(JSON.parseArray(specValue));
        }
        //商品详情信息图片
        ProductDetails productDetails = productDetailsMapper.selectByProductId(productId);
        if (ObjectUtil.isNotEmpty(productDetails)) {
            String imageUrls = productDetails.getImageUrls();
            String[] split = imageUrls.split(",");
            result.setDetailsImageUrlList(Arrays.asList(split));
        }

        //同一个商品下面的sku信息列表
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productSku.getProductId());
        //建立sku规格与skuId对应关系
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });
        result.setSkuSpecValueMap(skuSpecValueMap);

        return result;
    }
}
